package com.youxue.teachin.common.utils;

import android.util.Log;
import com.Navigator;
import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.AbstractErrorHandler;
import com.library.uiframework.utils.MToast;
import com.youxue.teachin.user.userSystem.UserSystem;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-07-02 20:56
 * @version: 9.1.0
 */
public class SystemErrorHandler extends AbstractErrorHandler {

    private String TAG = "SystemErrorHandler";

    public SystemErrorHandler() {
        super(null);
    }

    @Override
    protected boolean doHandle(int code, String message) {
        /**
         * 0 操作成功
         1 操作失败
         2 系统错误
         3 请求参数错误
         4 数据不存在
         100 用户未登录
         101 登录错误
         102 没有操作权限
         103 接口认证失败

         110001 手机号已注册
         110002 验证码错误
         110003 验证码过期
         110004 手机号未注册
         110005 用户不存在
         */
        switch (code) {
            case 0:
                break;
            case 2:
                MToast.getInstance().setText(message);
                break;
            case 1:
            case 3:
            case 4:
                MToast.getInstance().setText("系统错误");
                Log.d(TAG, "doHandle: code="+code+" message="+message);
                break;
            case 100:
                //if (!PreferencesUtils.getBoolean(PreContent.LOOK_AROUND, false)) {
                    new Navigator.Builder()
                        .setPageName("login")
                        .build().open();
                    //清楚token内存缓存
                    ApiServiceManager.clearToken();
                //} else {
                //    MToast.getInstance().setText("请登录！");
                //}
                break;
            case 101:
                //if (!PreferencesUtils.getBoolean(PreContent.LOOK_AROUND, false)) {
                    new Navigator.Builder()
                        .setPageName("login")
                        .build().open();
                    //清除taken内存、磁盘缓存
                    UserSystem.getInstance().clearToken();
                //}
                break;
            case 102:
            case 103:
            case 110001:
            case 110002:
            case 110003:
            case 110004:
            case 110005:
                MToast.getInstance().setText(message);
                Log.d(TAG, "doHandle: code="+code+" message="+message);
                break;
            default:
                break;
        }
        return false;
    }
}
